In computer science, a readers-writer or shared-exclusive lock (also known as the multiple readers / single-writer lock[1] or the multi-reader lock,[2] or by typographical variants such as readers/writers lock) is a synchronization primitive that solves one of the readers-writers problems. A readers-writer lock is like a mutex, in that it controls access to a shared resource, allowing concurrent access to multiple threads for reading but restricting access to a single thread for writes (or other changes) to the resource. A common use might be to control access to a data structure in memory that can't be updated atomically but isn't valid (and shouldn't be read by another thread) until the update is complete.
One potential problem with a conventional RW lock is that it can lead to write-starvation if contention is high enough, meaning that as long as at least one reading thread holds the lock, no writer thread will be able to acquire it. Since multiple reader threads may hold the lock at once, this means that a writer thread may continue waiting for the lock while new reader threads are able to acquire the lock, even to the point where the writer may still be waiting after all of the readers which were holding the lock when it first attempted to acquire it have finished their work in the shared area and released the lock. To avoid writer starvation, a variant on a readers-writer lock can be constructed which prevents any new readers from acquiring the lock if there is a writer queued and waiting for the lock, so that the writer will acquire the lock as soon as the readers which were already holding the lock are finished with it.[3] The downside is that it's less performant because each operation, taking or releasing the lock for either read or write, is more complex, internally requiring taking and releasing two mutexes instead of one.[4][3] This variation is sometimes known as a "write-preferring" or "write-biased" readers-writer lock.[5][6]
Readers-writer locks are usually constructed on top of mutexes and condition variables, or on top of semaphores.
The read-copy-update (RCU) algorithm is one solution to the readers-writers problem. RCU is wait-free for readers. The Linux-Kernel implements a special solution for few writers called seqlock.
A read/write lock pattern or simply RWL is a software design pattern that allows concurrent read access to an object but requires exclusive access for write operations.
In this pattern, multiple readers can read the data in parallel but an exclusive lock is needed while writing the data. When a writer is writing the data, readers will be blocked until the writer is finished writing.
Note that operations(either read or write) which you want to allow in parallel should grab the lock in read mode, and operations(either read or write) that you want to be exclusive should grab the lock in write mode.
pthread_rwlock_t
and associated operations.[7]System.Threading.ReaderWriterLockSlim
lock for C# and other .NET languages.[12]boost::shared_mutex
in read/write lock the Boost C++ Libraries.[13]java.util.concurrent.locks.ReentrantReadWriteLock
Java readers-writer lock implementation offers a "fair" modejava.util.concurrent.locks.ReadWriteLock
java.util.concurrent.locks.ReentrantReadWriteLock